from scipy.io import arff
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import warnings

warnings.filterwarnings('ignore')  # "error", "ignore", "always", "default", "module" or "once"


# 从文件读取数据
def load_data(filenames):
    x_train = pd.DataFrame()
    for fileName in filenames:
        data, meta = arff.loadarff(fileName)
        x = pd.DataFrame(data)
        x_train = pd.concat([x_train, x], ignore_index=True)
    y_train = x_train['Defective']
    y_train = mapit(y_train)
    del x_train['Defective']
    x_train = x_train.values
    return x_train, y_train


# 将布尔值转换为0和1
def mapit(vector):
    s = np.unique(vector)

    mapping = pd.Series([x[0] for x in enumerate(s)], index=s)
    vector = vector.map(mapping)
    return vector


# 从特征向量中抽取两个属性用于数据可视化
def show_data(x, y, title):
    negative = np.where(y > 0)  # 有缺陷
    positive = np.where(y < 1)  # 无缺陷

    plt.scatter(x[positive, 0], x[positive, 5], color='g', marker='.', label='positive')
    plt.scatter(x[negative, 0], x[negative, 5], color='r', marker='x', label='negative')
    plt.xlim(0, 200)
    plt.ylim(0, 80)
    plt.xlabel('LOC_TOTAL numeric', fontsize=9)
    plt.ylabel('NUM_UNIQUE_OPERANDS numeric', fontsize=9)
    plt.title(title)
    plt.legend()
    plt.show()


def load_file_data(filename):
    data, meta = arff.loadarff(filename)
    X = pd.DataFrame(data)
    Y = X['Defective']
    Y = mapit(Y)
    del X['Defective']
    X = X.values

    # 训练集
    x_train = X[0:int(X.shape[0] * 0.6), :]
    y_train = Y[0:int(Y.shape[0] * 0.6)]

    # 验证集
    x_cross = X[int(X.shape[0] * 0.6) + 1: int(X.shape[0] * 0.8), :]
    y_cross = Y[int(Y.shape[0] * 0.6) + 1: int(Y.shape[0] * 0.8)]

    # 测试集
    x_test = X[int(X.shape[0] * 0.8 + 1):, :]
    y_test = Y[int(Y.shape[0] * 0.8) + 1:]
    return x_train, y_train, x_cross, y_cross, x_test, y_test


x_train, y_train, x_cross, y_cross, x_test, y_test = load_file_data('./dataset/ar1.arff')
